<template>
  <div v-loading="isloading" class="fenleicontainer">
    <div v-if="(role = '市州用户')">
      <div class="header vm">
        <div>
          <i class="el-icon-d-arrow-right"></i>村庄规划质量提升文件上传(<span
            class="el-upload__tip"
            slot="tip"
            >1次只能上传1个xls/pdf文件</span
          >)
        </div>
        <el-button
          disabled
          type="success"
          icon="el-icon-download"
          @click="downloadExcel"
          >下载工作台账模板</el-button
        >
      </div>
      <div class="button-box">
        <el-upload
          class="upload-demo"
          action=""
          :file-list="tzpdf"
          :http-request="ledgerPDFUp"
          :limit="1"
          accept=".pdf"
        >
          <el-button disabled type="success" icon="el-icon-link"
            >上传村庄规划质量提升工作台账(pdf)</el-button
          >
        </el-upload>
      </div>

      <div style="float: right; margin-bottom: 20px">
        <el-button
          disabled
          type="danger"
          icon="el-icon-link"
          @click="submitFiles()"
          >确定上传</el-button
        >
      </div>
    </div>

    <div class="header">
      <i class="el-icon-d-arrow-right"></i>村庄规划质量提升工作上报记录
    </div>
    <el-table :data="tableData" border style="width: 100%" class="eltable">
      <el-table-column label="序号" width="100" prop="id">
        <template slot-scope="{ $index }">
          <span>{{ $index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="upload" label="更新时间" width="180" />
      <el-table-column prop="area" label="上传市州" />

      <el-table-column prop="tzpdf" label="工作台账pdf">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="dark"
            content="查看工作台账pdf"
            placement="bottom"
          >
            <a :href="getfile(scope.row.tzpdf)" download
              ><i class="iconfont icon-hetong"></i
            ></a>
          </el-tooltip>
        </template>
      </el-table-column>

      <!-- <el-table-column label="操作 " width="200" fixed="right" v-if="role=='区县用户'">
            <template scope="scope">
                <el-button type="danger" size="mini" @click="zhuijiaOpen(scope.row)" plain><i class="el-icon-plus"></i>追加</el-button>
            </template>
        </el-table-column> -->
    </el-table>
    <el-pagination
      v-if="tableData.length > 0"
      class="elpagi"
      :total="total"
      @size-change="getData"
      @current-change="getData"
      @prev-click="getData"
      @next-click="getData"
      :current-page.sync="listquery.currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size.sync="listquery.pageSize"
      layout="total,sizes, prev, pager, next, jumper"
    />
  </div>
</template>

<script>
import { apply1, getlist } from "@/api/uprecord.js";
import { picHost } from "@/settings.js";
import openMixin from "@/mixins/open.js";
export default {
  data() {
    return {
      tableData: [],
      isloading: false,
      tzpdf: [],
      listquery: {
        pageSize: 10,
        currentPage: 1,
        type: "c",
      },
      total: 0,
      role: "",
    };
  },
  mounted() {
    this.role = this.$store.getters.user.roles[0].name;
    this.getData();
  },
  mixins: [openMixin],
  methods: {
    //下载模板
    downloadExcel() {
      window.open(picHost + "村庄规划质量提升工作台账示例.xlsx");
    },

    ledgerPDFUp(data) {
      this.tzpdf.push(data.file);
    },

    reset() {
      this.tzpdf = [];
    },

    submitFiles() {
      if (this.tzpdf.length == 0) {
        this.$notify.error({
          title: "错误",
          message: "村庄规划质量提升工作台账（pdf）不能为空",
        });
        return;
      } else {
        var data = new FormData();
        data.append("year", "2023");
        data.append("pdf", this.tzpdf[0]);

        this.isloading = true;
        apply1(data)
          .then((v) => {
            if (v.success) {
              this.$notify({
                title: "成功",
                message: "上传成功",
                type: "success",
              });
              this.reset();
              this.getData();
            } else {
              this.$notify.error({
                title: "错误",
                message: v.message,
                duration: 100000,
              });
            }
            this.isloading = false;
          })
          .catch((err) => {
            this.$notify.error({
              title: "错误",
              message: err.response.data.message,
              duration: 100000,
            });
            this.isloading = false;
          });
      }
    },

    async getData() {
      this.isloading = true;
      let res = await getlist(this.listquery);
      this.tableData = res.data.content;
      this.total = res.data.totalElements;
      this.isloading = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.fenleicontainer {
  margin: 5px;
  padding-bottom: 10px;
  overflow-y: auto;
  position: relative;
  height: 100%;

  .button-box {
    margin-top: 10px;
    margin-bottom: 10px;
    display: flex;

    button {
      margin-right: 10px;
    }
  }
  .vm {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .header {
    line-height: 45px;
    margin-right: 5px;
    font-size: 15px;
    letter-spacing: 0.5px;
    font-weight: 600;
    color: #4778c7;

    i {
      color: #4778c7;
    }
  }
}
</style>
